﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.ComponentInterfaces;
using GameCommon.ManagerInterfaces;

using Menu.Common;

using Microsoft.Xna.Framework;

using GlobalServices = GameCommon.Services.Service;

#endregion

namespace Menu.Components
{
    internal class TextInput : Text, ITextHitBox, IUpdatable
    {
        private readonly Dimension inputDimension;

        private string inputString;

        private int inputX;

        public TextInput(string id, Text objectToCopy, FontEnum font, FontSizeEnum size)
            : base(id, objectToCopy, font, size)
        {
            this.inputDimension = (Dimension)this.Dimension.Clone();
        }

        public int Length { get; set; }

        public InputController InputController { get; set; }

        public int InputX
        {
            get
            {
                return this.inputX;
            }

            set
            {
                this.inputX = value;
                this.inputDimension.X += this.InputX;
            }
        }

        /// <summary>
        ///     Removes Event
        /// </summary>
        public override void Dispose()
        {
            base.Dispose();
            this.InputController.Dispose();
        }

        public void Select()
        {
            this.Selected = true;
            this.InputController.Begin(this.inputString);
            this.Opacity = 0.5f;
        }

        public void UnSelect()
        {
            this.Selected = false;
            this.InputController.End();
            this.Opacity = 1;
        }

        public override string ToString()
        {
            return string.Format("TextInput, Id: {0}", this.Id);
        }

        public void Update(GameTime gameTime)
        {
            if (this.InputController.IsActive)
            {
                this.inputString = this.InputController.Input;
            }
        }

        public override void Draw(IDrawingManager manager, GameTime gameTime)
        {
            base.Draw(manager, gameTime);
            this.DrawInputField(manager);
        }

        public override void Settle()
        {
            this.inputDimension.Y = this.Dimension.Y;
        }

        private void DrawInputField(IDrawingManager manager)
        {
            var inputValue = this.inputString;
            if (this.InputController.IsActive)
            {
                inputValue += "_";
            }

            this.DrawingService.DrawFont(manager, this.Font, this.inputDimension, inputValue, 1f, this.Layer);
        }
    }
}